package cn.knightzz.service.impl;

import cn.knightzz.entity.SysExamine;
import cn.knightzz.entity.SysMember;
import cn.knightzz.entity.SysUser;
import cn.knightzz.mapper.SysExamineMapper;
import cn.knightzz.service.SysExamineService;
import cn.knightzz.service.SysUserService;
import cn.knightzz.vo.ExamineCountVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 考核表, 主要记录党员做的好人好事 服务实现类
 * </p>
 *
 * @author knightzz
 * @since 2021-05-14
 */
@Service
public class SysExamineServiceImpl extends ServiceImpl<SysExamineMapper, SysExamine> implements SysExamineService {

    @Resource
    SysExamineMapper examineMapper;

    @Resource
    SysUserService userService;

    /**
     * 添加考核信息
     *
     * @param examine
     * @return
     */
    @Override
    public Boolean addExamine(SysExamine examine) {
        try {
            return examineMapper.insert(examine) !=0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 更新考核信息
     *
     * @param examine
     * @return
     */
    @Override
    public Boolean updateExamine(SysExamine examine) {
        try {
            return examineMapper.updateById(examine) !=0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删除考核信息
     *
     * @param id
     * @return
     */
    @Override
    public Boolean deleteExamine(Long id) {
        try {
            return examineMapper.deleteById(id) !=0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param query
     * @return
     */
    @Override
    public IPage<SysExamine> selectExamine(Long pageNum, Long pageSize, String query) {
        Page<SysExamine> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNum);

        QueryWrapper<SysExamine> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("member_name", query);

        try {
            return examineMapper.selectPage(page, queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 以党员为单位分组查询考核评分
     *
     * @param page
     * @param pageNum
     * @param pageSize
     * @param userId
     * @param query
     * @return
     */
    @Override
    public IPage<ExamineCountVO> selectCountExamine(Long pageNum, Long pageSize,
                                                    Long userId, String query) {

        SysUser userById = userService.getUserById(userId);

        Page<ExamineCountVO> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNum);

        try {
            return examineMapper.selectCountExamine(page, userById.getAddress(), query);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
